package org.ims.inv.model.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import cn.trymore.core.model.ModelBase;
import cn.trymore.core.util.UtilCollection;
import cn.trymore.core.util.UtilString;

/**
 * Model: 系统用户
 * 
 * @author Jeccy.Zhao
 *
 */
public class ModelAppUser
extends ModelBase implements UserDetails
{

	/**
	 * Serial UID
	 */
	private static final long serialVersionUID = 3882482262851009346L;
	
	/**
	 * 已删除标志
	 */
	public static final Integer FLAG_DEL = Integer.valueOf(1);
	
	/**
	 * 未删除标志
	 */
	public static final Integer FLAG_UNDEL = Integer.valueOf(0);
	
	/**
	 * 正常状态
	 */
	public static final Integer STAT_NORMAL = Integer.valueOf(1);
	
	/**
	 * 冻结状态
	 */
	public static final Integer STAT_FROZEN = Integer.valueOf(0);
	
	/**
	 * 超级用户ID
	 */
	public static final Long SUPER_USER_ID = new Long(1L);
	
	/**
	 * 超级用户名
	 */
	public static final String SUPER_USER_NAME = "super_root";
	
	/**
	 * 用户员工号
	 */
	private String userNo;
	
	/**
	 * 用户名
	 */
	private String username;
	
	/**
	 * 用户电话
	 */
	private String userPhoneNumber;
	
	/**
	 * 用户密码
	 */
	private String userPassword;
	
	/**
	 * 状态 ( 1=激活,0=冻结)
	 */
	private Integer userStatus = STAT_NORMAL;
	
	/**
	 * 用户角色
	 */
	//private ModelAppRole userRole;
	private String userRole;
	
	
	/**
	 * 最后一次登录IP地址
	 */
	private String lastLogonIP;
	
	/**
	 * 最后一次登录时间
	 */
	private Date lastLogonTime;

	/**
	 * 删除标记 (1=已删除, 0=未删除)
	 */
	private Integer delFlag = FLAG_UNDEL;
	
	/**
	 * 过滤超级管理员账号
	 */
	private Boolean isSuperFilter;
	
	/**
	 * 用户资料
	 */
	private ModelAppUserProfile userProfile;
	
	/**
	 * 用户邮箱
	 */
	private String userMail;
	
	/**
	 * 用户拥有的权限
	 */
	private List<ModelAppRole> userRoles =  new ArrayList<ModelAppRole>();
	
	/**
	 * 是否拥有给定权限
	 * 
	 * @param roleId
	 * @return
	 */
	public boolean containsRole (String roleId)
	{
		if (UtilString.isNotEmpty(roleId) && UtilCollection.isNotEmpty(userRoles))
		{
			for (ModelAppRole userRole : userRoles)
			{
				if (userRole.getId().equals(roleId))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 获取权限名
	 * 
	 * @return
	 */
	public String getRoleName ()
	{
		if (UtilCollection.isNotEmpty(userRoles))
		{
			String[] roleNames = new String[userRoles.size()];
			for (int i = 0, size = userRoles.size(); i < size; i++)
			{
				roleNames[i] = userRoles.get(i).getRoleName(); 
			}
			
			return UtilString.join(roleNames, ",");
		}
		
		return "";
	}
	
	/**
	 * 获取权限Id
	 * 
	 * @return
	 */
	public String getRoleId ()
	{
		if (UtilCollection.isNotEmpty(userRoles))
		{
			String[] roleIds = new String[userRoles.size()];
			for (int i = 0, size = userRoles.size(); i < size; i++)
			{
				roleIds[i] = userRoles.get(i).getId();
			}
			
			return UtilString.join(roleIds, ",");
		}
		
		return "";
	}
	
	public String getUserPassword()
	{
		return userPassword;
	}

	public void setUserPassword(String userPassword)
	{
		this.userPassword = userPassword;
	}

	public Integer getUserStatus()
	{
		return userStatus;
	}

	public void setUserStatus(Integer userStatus)
	{
		this.userStatus = userStatus;
	}

	public String getUserRole()
	{
		return userRole;
	}

	public void setUserRole(String userRole)
	{
		this.userRole = userRole;
	}

	public Date getLastLogonTime()
	{
		return lastLogonTime;
	}

	public void setLastLogonTime(Date lastLogonTime)
	{
		this.lastLogonTime = lastLogonTime;
	}

	public void setUserNo(String userNo)
	{
		this.userNo = userNo;
	}

	public String getUserNo()
	{
		return userNo;
	}

	public void setDelFlag(Integer delFlag)
	{
		this.delFlag = delFlag;
	}

	public Integer getDelFlag()
	{
		return delFlag;
	}

	public void setLastLogonIP(String lastLogonIP)
	{
		this.lastLogonIP = lastLogonIP;
	}

	public String getLastLogonIP()
	{
		return lastLogonIP;
	}

	public void setUserPhoneNumber(String userPhoneNumber)
	{
		this.userPhoneNumber = userPhoneNumber;
	}

	public String getUserPhoneNumber()
	{
		return userPhoneNumber;
	}
	
	public boolean isSuperUser ()
	{
		return SUPER_USER_NAME.equals(this.username);
	}
	
	/**
	 * 是否为领导
	 * 
	 * @return
	 */
	public boolean isLeaderShip ()
	{
		if (UtilCollection.isNotEmpty(userRoles))
		{
			for (ModelAppRole userRole : userRoles)
			{
				if (ModelAppRole.ERoleProperty.LEADERSHIP.getValue().equals(userRole.getRoleProperty()))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 是否为助理
	 * 
	 * @return
	 */
	public boolean isAssistant ()
	{
		if (UtilCollection.isNotEmpty(userRoles))
		{
			for (ModelAppRole userRole : userRoles)
			{
				if ("ROLE_PROJECT_ASSISTANT".equals(userRole.getRoleKey()))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public GrantedAuthority[] getAuthorities()
	{
		if (this.getUsername().equals(SUPER_USER_NAME))
		{
			return new GrantedAuthority[] {new GrantedAuthorityImpl(ModelAppRole.ROLE_SUPER)};
		}
		
		String roleKey = getRoleKey();
		if (UtilString.isNotEmpty(roleKey))
		{
			String[] keys = roleKey.split(",");
			GrantedAuthority[] authority = new GrantedAuthority[keys.length];
			for (int i = 0, len = keys.length; i < len; i++)
			{
				authority[i] = new GrantedAuthorityImpl(keys[i]);
			}
			
			return authority;
		}
		
		return new GrantedAuthority[] {};
	}
	
	/**
	 * 获取权限Keys
	 * 
	 * @return
	 */
	public String getRoleKey()
	{
		if (this.isSuperUser())
		{
			return ModelAppRole.ROLE_SUPER;
		}
		else if (UtilCollection.isNotEmpty(userRoles))
		{
			List<String> roleKeys = new ArrayList<String>();
			for (ModelAppRole userRole : userRoles)
			{
				if (userRole.getRoleAdminFlag().equals(ModelAppRole.ERoleAdminFlag.ADMINI.getValue()))
				{
					roleKeys.add(ModelAppRole.ROLE_ADMIN);
				}
				else
				{
					roleKeys.add(userRole.getRoleKey());
				}
			}
			
			return UtilString.join(roleKeys, ",");
		}
		
		return null;
	}

	@Override
	public String getPassword()
	{
		return this.userPassword;
	}

	@Override
	public String getUsername()
	{
		return this.username;
	}

	@Override
	public boolean isAccountNonExpired()
	{
		return true;
	}

	@Override
	public boolean isAccountNonLocked()
	{
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired()
	{
		return true;
	}

	@Override
	public boolean isEnabled()
	{
		return STAT_NORMAL.equals(this.userStatus);
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public void setSuperFilter(Boolean isSuperFilter)
	{
		this.isSuperFilter = isSuperFilter;
	}

	public Boolean isSuperFilter()
	{
		return isSuperFilter;
	}

	public void setUserMail(String userMail)
	{
		this.userMail = userMail;
	}

	public String getUserMail()
	{
		return userMail;
	}

	public ModelAppUserProfile getUserProfile()
	{
		return userProfile;
	}

	public void setUserProfile(ModelAppUserProfile userProfile)
	{
		this.userProfile = userProfile;
	}

	public List<ModelAppRole> getUserRoles()
	{
		return userRoles;
	}
	
}
